// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    scroll:number=0;

    // LIFE-CYCLE CALLBACKS:

   onLoad () {

    this.scroll = 0;

    this.node.opacity = 50;

    //先监听鼠标按下
    this.node.on(cc.Node.EventType.MOUSE_DOWN, function () {

        this.node.opacity = 255;

        //再监听鼠标以偶当
        this.node.on(cc.Node.EventType.MOUSE_MOVE, this.move, this);

    }, this);

    //监听鼠标加入Hover
    this.node.on(cc.Node.EventType.MOUSE_ENTER, function () {

        this.node.opacity = 160;

    }, this);

    //先监听鼠标离开
    this.node.on(cc.Node.EventType.MOUSE_LEAVE, function () {

        this.node.opacity = 50;
        //再监听鼠标移动
        this.node.off(cc.Node.EventType.MOUSE_MOVE, this.move, this);
    }, this);

    //先监听鼠标抬起
    this.node.on(cc.Node.EventType.MOUSE_UP, function () {
        this.node.opacity = 50;

        //再监听鼠标移动
        this.node.off(cc.Node.EventType.MOUSE_MOVE, this.move, this);

        if (this._callback) {
            this._callback();
        }
    }, this);

    //监听鼠标滚轮滑动时
    this.node.on(cc.Node.EventType.MOUSE_WHEEL, function (event) {

        this.scroll += event.getScrollY();

        var h = this.node.height;

        this.scroll = cc.clampf(this.scroll, -2 * h, 0.7 * h);

        this.node.scale = 1 - this.scroll/h;

    }, this);

     //监听自定义事件
    this.node.on('say-hello', function (event) {

            console.log("我开始接收事件了："+event.detail.msg);

          });

    }
     protected  start () {

        //发射自定义事件
        this.node.emit('say-hello', {
          msg: 'Hello, this is Cocos Creator,我开始要发送自定义事件了',
        });
      }

  private move(event){

    this.node.x+=event.getDelataX();

    this.node.y+=event.getDeltaY();
  }

    // update (dt) {}
}
